package com.bookcity.front.webfront.ctrl;

import com.bookcity.front.webfront.config.MyStore;
import com.bookcity.front.webfront.domain.*;
import com.bookcity.front.webfront.service.IBookService;
import com.bookcity.front.webfront.service.ISectionService;
import com.bookcity.front.webfront.service.ITypeService;
import com.bookcity.front.webfront.vo.BookVo;
import com.bookcity.front.webfront.vo.IndexPage;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
@RestController
@RequestMapping("book")
@CrossOrigin("*")
public class BookController extends BaseController {
    @Autowired
    private MyStore redisTemplate;
    //>>>
    @Autowired
    private ITypeService typeService;
    @Autowired
    private IBookService bookService;
    @Autowired
    private ISectionService sectionService;

    //全部的小说+分类+搜索
    @GetMapping("index")
    @ApiOperation("首页小说信息，支持搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "1", dataTypeClass = int.class)
            ,
            @ApiImplicitParam(name = "pageSize", value = "10", dataTypeClass = int.class)

    })
    public TableDataInfo index(@RequestBody BookVo book) {
        startPage();
        List<Book> list = bookService.selectSearchBook(book);
        return getDataTable(list);
    }

    @ApiOperation("首页根据小说分类查找")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "1", dataTypeClass = int.class)
            ,
            @ApiImplicitParam(name = "pageSize", value = "10", dataTypeClass = int.class)

    })
    @GetMapping("type/{typeid}")
    public TableDataInfo searchByType(@PathVariable("typeid") Long typeid) {
        startPage();
        List<Book> list = bookService.selectByType(typeid);
        return getDataTable(list);
    }
    //最新
    @GetMapping("newest")
    @ApiOperation("查看新书，支持分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "1", dataTypeClass = int.class)
            ,
            @ApiImplicitParam(name = "pageSize", value = "10", dataTypeClass = int.class)

    })
    public TableDataInfo newest(@RequestBody Book book) {
        startPage();
        List<Book> list = bookService.selectBookNewest(book);
        return getDataTable(list);
    }

    //热度最高
    @GetMapping("hot")
    @ApiOperation("查看热度最高的书，支持分页:pageNum,pageSize")

    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "1", dataTypeClass = int.class)
            ,
            @ApiImplicitParam(name = "pageSize", value = "10", dataTypeClass = int.class)

    })
    public TableDataInfo hot(@RequestBody Book book) {
        startPage();
        List<Book> list = bookService.selectBookHot(book);
        return getDataTable(list);
    }

    @ApiOperation("获得所有分类")
    @GetMapping("types")
    public AjaxResult types() {
        List<Type> types = typeService.selectTypeList(null);
        return AjaxResult.success(types);
    }

    @ApiOperation("针对特定的小说产生一个推广码")
    @PostMapping("genPromotion")
    public AjaxResult genPromotion(@RequestBody Promotion b) {
        Long userid=getCurrentMemeber().getId();
        b.setMemid(String.valueOf(userid));
        Promotion bp = bookService.genPromotion(b);
        return AjaxResult.success("欢迎推广，推广码"+bp.getId()+",请去QQ群复制吧");
    }
    //
    @ApiOperation("小说详细信息")
    @GetMapping("/book/{id}")
    //@PreAuthorize("hasAuthority('douyinuser')")
    @PreAuthorize("@myAccessDecisionService.hasPermission('douyinuser')")
    //@PreAuthorize("hasRole('douyinuser')")
    public AjaxResult findBook(@PathVariable("id") Long id){
        Book book = this.bookService.selectBookById(id);
        return AjaxResult.success(book);
    }
    @ApiOperation("小说查找所有章节,地址栏包含bookid参数")
    @GetMapping("/secs/{bid}")
    public AjaxResult findSecs(@PathVariable("bid") Long bid){
        List<Section> sections = sectionService.selectSections(bid);
        return AjaxResult.success(sections);
    }
    @ApiOperation("查看查找内容,地址栏包含secid参数")
    @GetMapping("/secshow/{secid}")
    public AjaxResult findSec(@PathVariable Long sid){
       Section s = sectionService.selectSectionById(sid);
        return AjaxResult.success(s);
    }
    @ApiOperation("查看小说当前章节的下一章,600表示要充值")
    @GetMapping("/secnext/{secid}")
    public AjaxResult findNextSec(@PathVariable Long sid){
        //通过token获得user
        //Long userid=1L;

        Long userid=getCurrentMemeber().getId();

        Member member=new Member();//...
        member.setId(userid);
        Section s = sectionService.selectNextSection(sid);
        if(s==null){
            return AjaxResult.error("最后一章了，没有了");
        }
        if(s.getIsfee()){
            if(member.getExpiredate()==null||member.getExpiredate().compareTo(new Date())<0){
                return AjaxResult.error(600,"未充值称为会员或者会员已经到期");
            }
        }
        return AjaxResult.success(s);
    }

    private Member getCurrentMemeber(){
        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        String token = request.getHeader("token");
        return (Member) redisTemplate.get(token);
    }
}
